package com.bocai.yuweiwangluo.activity;


import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.yuweiwangluo.R;
import com.bocai.yuweiwangluo.View.XListView;
import com.bocai.yuweiwangluo.base.BaseActivity;
import com.bocai.yuweiwangluo.bean.VenueListBean;
import com.bocai.yuweiwangluo.presenter.VenueListPresenter;
import com.bocai.yuweiwangluo.viewModel.IVenueListView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

/**
 * 搜索场馆界面
 */
public class SearchVenueActivity extends BaseActivity implements View.OnClickListener,IVenueListView,XListView.IXListViewListener,AdapterView.OnItemClickListener{
    @ViewInject(R.id.ll_nocotent)
    LinearLayout llnoContent;
    @ViewInject(R.id.ll_title_right)
    LinearLayout ll_title_right;
    @ViewInject(R.id.tv_title_right)
    TextView tv_title_right;
    @ViewInject(R.id.et_search)
    EditText et_search;
    @ViewInject(R.id.lv_searchvenue)
    XListView lvSerchVenue;
    @ViewInject(R.id.ll_refresh)
    LinearLayout llRefresh;
    private int page =1;
    private VenueListPresenter venueListPresenter;

    @Override
    protected int contentView() {
        return R.layout.activity_searchvenue;
    }

    @Override
    protected void setData() {
        ViewUtils.inject(this);
        getVenueListPresenter();
        ll_title_right.setOnClickListener(this);
        lvSerchVenue.setOnItemClickListener(this);
        lvSerchVenue.setPullLoadEnable(true);
        lvSerchVenue.setPullRefreshEnable(true);
        lvSerchVenue.setXListViewListener(this);
        tv_title_right.setText("取消");
        et_search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() == 0) {
                    tv_title_right.setText("取消");
                } else {
                    tv_title_right.setText("搜索");
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
        et_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    page=1;
                    venueListPresenter.getVenueList(page+"", "", "");
                }
                return false;
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ll_title_right:
                if (tv_title_right.getText().equals("取消")) {
                    this.finish();
                }else{
//                    tv_title_right.setText("取消");
                    page=1;
                    venueListPresenter.getVenueList(page+"", "", "");
                }
                break;
        }
    }

    @Override
    public String getSortType() {
        return "";
    }

    @Override
    public int getPage() {
        return page;
    }


    @Override
    public void load() {
        lvSerchVenue.stopRefresh();
        lvSerchVenue.stopLoadMore();
        lvSerchVenue.setRefreshTime("");
    }

    @Override
    public XListView getListView() {
        return lvSerchVenue;
    }

    @Override
    public LinearLayout getnoContent() {
        return llnoContent;
    }
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
        VenueListBean.Data data= (VenueListBean.Data) parent.getAdapter().getItem(position);
        Intent intent=new Intent(this,VenueBookActivity.class);
        intent.putExtra("id",data.getId());
        intent.putExtra("title",data.getTitle());
        intent.putExtra("content",data.getContent());
        intent.putExtra("local_x",data.getLocal_x());
        intent.putExtra("local_y",data.getLocal_y());
        startActivity(intent);
    }
    @Override
    public VenueListPresenter getVenueListPresenter() {
        venueListPresenter=new VenueListPresenter(this,this,this);
        return venueListPresenter;
    }

    @Override
    public void finishActivity() {
        this.finish();
    }

    @Override
    public TextView getTitleRight() {
        return tv_title_right;
    }

    @Override
    public LinearLayout getllRefresh() {
        return null;
    }

    @Override
    public String getVenue() {
        return et_search.getText().toString();
    }

    @Override
    public void onRefresh() {
        page=1;
        lvSerchVenue.setPullLoadEnable(true);
        venueListPresenter.getVenueList(page+"", "", "");
    }

    @Override
    public void onLoadMore() {
        page++;
        venueListPresenter.getVenueList(page+"", "", "");
    }
}
